package com.nari.ncps.screen.service.impl.al;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.al.AlOpermonEqpState;
import com.nari.ncps.screen.mapper.al.AlOpermonEqpStateMapper;
import com.nari.ncps.screen.service.al.AlOpermonEqpStateService;
import com.nari.ncps.screen.vo.al.AlOpermonEquipmentStateVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 设备状态表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Slf4j
@Service
public class AlOpermonEqpStateServiceImpl extends ServiceImpl<AlOpermonEqpStateMapper, AlOpermonEqpState> implements AlOpermonEqpStateService {

    @Override
    public List<AlOpermonEquipmentStateVO> getLatestEquipmentState() {
        log.info("开始查询设备状态数据");
        
        // 使用MyBatis Plus查询所有最新的设备状态（按年月日倒序）
        QueryWrapper<AlOpermonEqpState> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("year", "month", "day")
                .orderByAsc("aluminum_plant_type")
                .last("LIMIT 10");
        
        List<AlOpermonEqpState> stateList = this.list(queryWrapper);
        log.info("从数据库查询到{}条记录", stateList == null ? 0 : stateList.size());
        
        // 如果数据库中没有数据，返回mock数据
        if (stateList == null || stateList.isEmpty()) {
            log.info("数据库无数据，返回mock数据");
            return generateMockEquipmentState();
        }
        
        // 检查是否有有效数据
        boolean hasValidData = false;
        for (AlOpermonEqpState state : stateList) {
            if (state.getAluminumPlantType() != null && 
                (state.getElectrolyticCellRunning() != null || state.getRectifierUnitRunning() != null)) {
                hasValidData = true;
                break;
            }
        }
        
        if (!hasValidData) {
            log.info("数据库有记录但无有效数据，返回mock数据");
            return generateMockEquipmentState();
        }
        
        // 转换为VO对象列表
        List<AlOpermonEquipmentStateVO> voList = new ArrayList<>();
        
        for (AlOpermonEqpState state : stateList) {
            AlOpermonEquipmentStateVO vo = new AlOpermonEquipmentStateVO();
            
            // 根据铝厂类型设置铝厂名称
            String plantName = getPlantName(state.getAluminumPlantType());
            vo.setPlantName(plantName);
            
            // 设置设备状态数据
            vo.setElectrolyticCellStopped(state.getElectrolyticCellStopped());
            vo.setElectrolyticCellRunning(state.getElectrolyticCellRunning());
            vo.setRectifierUnitStopped(state.getRectifierUnitStopped());
            vo.setRectifierUnitRunning(state.getRectifierUnitRunning());
            
            voList.add(vo);
        }
        
        log.info("返回{}条设备状态数据", voList.size());
        return voList;
    }
    
    /**
     * 生成mock设备状态数据
     * 覆盖三个铝厂的设备状态信息
     */
    private List<AlOpermonEquipmentStateVO> generateMockEquipmentState() {
        log.info("生成mock设备状态数据");
        List<AlOpermonEquipmentStateVO> mockList = new ArrayList<>();
        
        // 霍煤鸿铝厂
        AlOpermonEquipmentStateVO plant1 = new AlOpermonEquipmentStateVO();
        plant1.setPlantName("霍煤鸿铝厂");
        plant1.setElectrolyticCellStopped(8);      // 停运8台
        plant1.setElectrolyticCellRunning(392);    // 运行392台，总共400台
        plant1.setRectifierUnitStopped(2);         // 停运2台
        plant1.setRectifierUnitRunning(38);        // 运行38台，总共40台
        mockList.add(plant1);
        
        // 扎哈淖尔铝厂
        AlOpermonEquipmentStateVO plant2 = new AlOpermonEquipmentStateVO();
        plant2.setPlantName("扎哈淖尔铝厂");
        plant2.setElectrolyticCellStopped(5);      // 停运5台
        plant2.setElectrolyticCellRunning(315);    // 运行315台，总共320台
        plant2.setRectifierUnitStopped(1);         // 停运1台
        plant2.setRectifierUnitRunning(31);        // 运行31台，总共32台
        mockList.add(plant2);
        
        // 白音华铝厂
        AlOpermonEquipmentStateVO plant3 = new AlOpermonEquipmentStateVO();
        plant3.setPlantName("白音华铝厂");
        plant3.setElectrolyticCellStopped(12);     // 停运12台
        plant3.setElectrolyticCellRunning(468);    // 运行468台，总共480台
        plant3.setRectifierUnitStopped(3);         // 停运3台
        plant3.setRectifierUnitRunning(45);        // 运行45台，总共48台
        mockList.add(plant3);
        
        return mockList;
    }
    
    /**
     * 根据铝厂类型获取铝厂名称
     *
     * @param aluminumPlantType 铝厂类型：0-霍煤鸿铝厂,1-扎哈淖尔铝厂,2-白音华铝厂
     * @return 铝厂名称
     */
    private String getPlantName(Byte aluminumPlantType) {
        if (aluminumPlantType == null) {
            return "未知铝厂";
        }
        
        switch (aluminumPlantType) {
            case 0:
                return "霍煤鸿铝厂";
            case 1:
                return "扎哈淖尔铝厂";
            case 2:
                return "白音华铝厂";
            default:
                return "未知铝厂";
        }
    }

}
